<%@page import="com.metacube.metaconnect.Service.UserInformationService"%>
<%@page import="com.metacube.metaconnect.Service.CommentsService"%>
<%@page import="com.metacube.metaconnect.Service.LikeService"%>
<%@page import="com.metacube.metaconnect.Service.PostService"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<style type="text/css">

#dhtmltooltip{
position: absolute;
width: 150px;
border: 2px solid black;
padding: 2px;
background-color: lightyellow;
visibility: hidden;
z-index: 100;
/*Remove below line to remove shadow. Below line should always appear last within this CSS*/
filter: progid:DXImageTransform.Microsoft.Shadow(color=gray,direction=135);
}

</style>

<link rel="Stylesheet" href="css/Posts.css" type="text/css" />
<script type="text/javascript" src="js/UserProfile.js">
</script>
<script type="text/javascript">
var textFieldInFocus="temp";
function focusComment(comment)
{
	if(comment.value=="comment here...."){
		
		comment.value="";
		comment.style.color="black";
	}
	textFieldInFocus = null;
}
function blurComment(comment)
{
	if(comment.value=="")
	{
		comment.value="comment here....";
		comment.style.color="gray";
	}
	textFieldInFocus = "temp";
	
}
function handleOnFocus(form_element)
{
	textFieldInFocus = null;
}
function handleOnBlur()
{
   textFieldInFocus = "temp";
}

function Ajax(){
var xmlHttp;
	try{	
		xmlHttp=new XMLHttpRequest();// Firefox, Opera 8.0+, Safari
	}
	catch (e){
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
		}
		catch (e){
		    try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("No AJAX!?");
				return false;
			}
		}
	}

xmlHttp.onreadystatechange=function(){
	if(xmlHttp.readyState==4 && textFieldInFocus=="temp"){
		document.getElementById('posts').innerHTML=xmlHttp.responseText;
		setTimeout('Ajax()',10000);
	}
}
xmlHttp.open("GET","Posts.jsp",true);
xmlHttp.send(null);
}

window.onload=function(){
	setTimeout('Ajax()',10000);
}
</script>

	
	
<%
if(session.getAttribute("emailId")==null)
{
	response.sendRedirect("index.jsp");
}
else
{
ArrayList posts = new PostService().getAllPosts();

Iterator iteratorOfPosts = posts.iterator();
String email = session.getAttribute("emailId").toString();

while(iteratorOfPosts.hasNext())
{	

	String imgUrl = iteratorOfPosts.next().toString();
	String userUrl = new UserInformationService().getProfilePicUrl(email);
	String name= iteratorOfPosts.next().toString();
	String post_content= iteratorOfPosts.next().toString();
	String postcontent="";
	int count=0;
	while(post_content.length()>50)
	{
		postcontent+=post_content.substring(count,count+49)+"<br/>";
		post_content=post_content.substring(49);
	}
	postcontent+=post_content;
	String date= iteratorOfPosts.next().toString();
	String post_id = iteratorOfPosts.next().toString();
	String email_id = iteratorOfPosts.next().toString();
	request.setAttribute("post_id",post_id);
	String numberOfLike = new LikeService().countLike(post_id);
	int othersLike = Integer.parseInt(numberOfLike)-1;
	boolean isPostLike = new LikeService().checkIfLike(post_id,email);
%>


	<table id="postContainer">
		<tr>
			<td valign="top"><img style="width:50px; height:50px; margin-right: 10px;" src="<%=imgUrl %>"></td>
			
			<td style=" vertical-align: top;" colspan="2">
				 <span style="float:left; font-weight: bold; margin-right: 20px; "><a href="OtherUserHome.jsp?userEmail=<%=email_id %>"><%=name %> </a> </span>
				 <%if(email.equals(email_id)){ %>
				<a style="float:right"; title="Delete Post" href="DeletePost?post_id=<%=post_id %>"><img src="Upload/delete.gif" >
				</a><%} %>
				 <span style="margin: 5px;font-size: 10pt; color: #878070; float:right;">At &nbsp; <%=date %> </span>
				
			</td>
		</tr>
		<tr>
			<td></td>
			<td align="left" width="300px" colspan=2 >
				<p style=" width:250px;" > <%=postcontent %> </p>	
			</td>
		</tr>
		
		<tr>
			<td></td>
			<td colspan="2" align="left"><%if(numberOfLike.trim().equals("1") && isPostLike){ %><font color=blue>You</font>&nbsp;Like this <%} %>
			<%if(!numberOfLike.trim().equals("0") && !isPostLike){ %><font color=blue><%=numberOfLike %></font>&nbsp;People Like this <%} %>
			<%if(!numberOfLike.trim().equals("1") && isPostLike){ %><font color=blue>You</font> and <font color=blue><%=othersLike %></font>&nbsp;People Like this <%} %>
			

			
				<%if(!isPostLike){ %>
				<a href="InsertLikeServlet?post_id=<%=post_id %>" style="margin: 5px;">Like</a>
				<%} %>
			</td>
		</tr>
		<%ArrayList comments = new CommentsService().getAllComments(post_id);
			Iterator iteratorOfComments = comments.iterator();
				while(iteratorOfComments.hasNext()){
					String imgCurl = iteratorOfComments.next().toString();
					String commentatorName= iteratorOfComments.next().toString();
					String comment_content= iteratorOfComments.next().toString();
					String commentDate= iteratorOfComments.next().toString();
					String comment_id = iteratorOfComments.next().toString();
					String emailid = iteratorOfComments.next().toString();
					
					String commentcontent="";
					 count=0;
					while(comment_content.length()>30)
					{
						commentcontent+=comment_content.substring(count,count+29)+"<br/>";
						comment_content=comment_content.substring(29);
					}
					commentcontent+=comment_content;
					%>
			<tr>
				<td></td>
			    <td colspan="2">
				  <table  id="commentTable">
				     <tr>
					     <td valign=top rowspan=2><img src="<%=imgCurl %>" width="25" height="25"> </td>
						 <td valign=top >
						 <span style="float:left;  font-weight: bold; font-size:10pt;"><a href="OtherUserHome.jsp?userEmail=<%=emailid %>"> <%=commentatorName %></a></span> 
						 <%if(email.equals(emailid.trim()) || email_id.equals(email)){ %>
				         <a style="float:right;margin:2px 0 0px 5px;" title="Delete Comment" href="DeleteComment?comment_id=<%=comment_id %>"><img src="Upload/deletecomment.gif"  >
				         </a><%} %>
						 <span style=" margin-left:210px; float:right; font-size: 10pt;  color:#878070"> At &nbsp; <%=commentDate %> </span>
						 </td>
					 </tr>
					  <tr> 
					    <td align="left" colspan="2">
						<P style="width:200px;"> <%=commentcontent %> </P>
						</td>
					 </tr>
				  </table>
				
				</td>
			</tr>
					<%
				}					
		%>

		<tr>
				<td></td>
				<td valign="top"><img src="<%=userUrl %>" width="25" height="25"></td>
				<td colspan="">
					<form action="InsertCommentServlet?post_id=<%=post_id %>" method="post">
						<input type="text"  name="comment" maxlength="300" id="comment" value="comment here...." onfocus='focusComment(this)' onblur='blurComment(this)'  style="color: gray" size="60"  />
						
					</form>
			   </td>
		   </tr>
		   <tr>
				<td colspan="3">
				  <hr>
				</td>
				
		   </tr>
	</table>
<%
}%>
<%
}
%>
